<?php

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

require_once  __DIR__ . '/includes/authenticate.php';

include_once __DIR__ . '/db_bb_connect.php';

include __DIR__ . '/mainpage.php';

?>

    <div class="container-fluid">
    <div class="row">
      <div class="col-md-4"></div>
      <div class="col-md-4" id="success_message"> 
        <h2>Add Article</h2>

        <form method="post" class="hidden-print"> 
<div class="form-group">
  <input type="text" name="title" class="form-control" id="title">
</div>

<div class="form-group">
<textarea id="mytextarea" name="text">
</textarea>
</div>

<div class="form-group">
  <button class="btn btn-success btn-lg" id="submit" name="submit">Add</button>
</div>

    </form>

      </div>
      <div class="col-md-4"></div>
    </div>
 </div>

  <script type="text/javascript" src="./js/tinymce/tinymce.min.js"></script>

<script type="text/javascript">

  tinymce.init({
        selector: "#mytextarea",
        theme: "modern",
        width: 680,
        height: 300,
        plugins: [
         "advlist autolink link image lists charmap print preview hr anchor pagebreak",
         "searchreplace wordcount visualblocks visualchars insertdatetime media nonbreaking",
         "table contextmenu directionality emoticons paste textcolor responsivefilemanager code"
        ],
        toolbar1: "undo redo | bold italic underline | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | styleselect",
        toolbar2: "| responsivefilemanager | link unlink anchor | image media | forecolor backcolor  | print preview code ",
        image_advtab: true,
        relative_urls: false,
        image_caption: true,
   
        external_filemanager_path:"filemanager/",
        filemanager_title:"Responsive Filemanager" ,
        external_plugins: { "filemanager" : "../../filemanager/plugin.min.js"}
  });
  
    $('#submit').on('click', function(event){

    tinymce.triggerSave();

    var activeEditor = tinyMCE.get('mytextarea');

    event.preventDefault();
    var form_data = $(this.form).serialize();

    $.ajax({
    url: "./includes/handlers/article_add_handler.php",
    method: "POST",
    data: form_data,
    beforeSend:function(){
     $('#submit').val('Submitting...');
    },
    success:function(data){
     if(data != '')
     {
      $('#title').val('');
      activeEditor.setContent('');
      $('#success_message').html(data);

      $('#submit').attr("disabled", false);
      $('#submit').val('Add');
     }
    }, 
    error: function (xhr, status, error) {
          alert(xhr.responseText); 
    }
    });
    });


  </script>